package com.lyxs916.testlibrary.recyclertest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.lyxs916.appbaselibrary.core.activity.BaseMvpActivity;
import com.lyxs916.testlibrary.R;
import com.lyxs916.adapter.ViewHolder;
import com.lyxs916.adapter.recyclerview.CommonAdapter;
import com.lyxs916.xrecycleview.SuperRecyclerView;
import com.lyxs916.utilslibrary.ui.ToastUtils;
import com.lyxs916.utilslibrary.ui.divider.HorizontalDividerItemDecoration;
import com.lyxs916.utilslibrary.view.SupperToolBar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 说明:自动加载RecyclerView 请求测试.这是继承与BaseMvpActivity实现
 * 作者： 杨阳; 创建于：  2017-06-14  10:02
 */
public class ListTestActivity extends BaseMvpActivity<IListTestParentView.IListTestView, ListTestPresenter> implements IListTestParentView.IListTestView {
    SupperToolBar toolbar;
    SuperRecyclerView switchRootId;
    ArrayList<ListTestBean> mDatas = new ArrayList<>();
    CommonAdapter adapter;
    ViewGroup miewGroup;

    public static void startUi(Context context) {
        Intent intent = new Intent(context, ListTestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return R.layout.test_activity_list;
    }

    @Override
    public void parseIntent(Intent intent) {

    }


    @Override
    public void initView() {
        toolbar = (SupperToolBar) findViewById(R.id.toolbar);
        switchRootId = (SuperRecyclerView) findViewById(R.id.superRecyclerView);
        miewGroup = (ViewGroup) findViewById(R.id.rootView);

        toolbar.setTitle("自动加载RecyclerView 请求测试");
        toolbar.setBack(this);
        switchRootId.setOnLoaddingListener(this);
        switchRootId.setOnRefreshListener(this);
        switchRootId.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        switchRootId.getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).sizeResId(R.dimen.dp_5).colorResId(R.color.gray_ee).build());
        switchRootId.setAdapter(adapter = new CommonAdapter<ListTestBean>(this, R.layout.test_item_test, mDatas) {
            @Override
            public void convert(ViewHolder holder, ListTestBean s) {
                holder.setText(R.id.test, "第" + holder.getmPosition() + "条： " + s.getHourNumber());
            }
        });

        presenter.requestHttpData(true, miewGroup);


//        //加载更多
//        switchRootId.setOnLoaddingListener(new OnLoaddingListener() {
//            @Override
//            public void onLoadding() {
//                presenter.requestHttpData(true, miewGroup);
//            }
//        });
//        //刷新
//        switchRootId.getSwipeRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mDatas.clear();
//                adapter.notifyDataSetChanged();
//                presenter.requestHttpData(true, miewGroup);
//            }
//        });

    }


    @Override
    public ListTestPresenter initPresenter() {
        return new ListTestPresenter();
    }

    @Override
    public void resultData(List<ListTestBean> data) {
//        switchRootId.getSwipeRefreshLayout().setRefreshing(false);
//        switchRootId.getStatusChangListener().complete();


        mDatas.addAll(data);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void coreOnLoadingBarListener(View parent) {
        ToastUtils.getToastLong("123456");
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public SuperRecyclerView getSuperRecyclerView() {
        return switchRootId;
    }

    @Override
    public <T> Collection<T> getValidData(Collection<T> c) {
        return switchRootId.getPagingHelp().getValidData(c);
    }

    @Override
    public void clearPagingData() {
        switchRootId.getPagingHelp().clear();
    }

    @Override
    public int getPageindex() {
        return switchRootId.getPagingHelp().getPageindex();
    }

    @Override
    public int getPageCount() {
        return switchRootId.getPagingHelp().getPageCount();
    }

    @Override
    public void onRefresh() {
        adapter.notifyDataSetChanged();
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public void onLoadding() {
        presenter.requestHttpData(false, miewGroup);
    }

    @Override
    public void clearData() {
        mDatas.clear();
    }
}
